package View.GridDialog;

import View.MainFrame;

import javax.swing.*;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: Andrew
 * Date: 17.03.12
 * Time: 18:53
 * To change this template use File | Settings | File Templates.
 */
public class GridDialog  extends JDialog implements ActionListener {
    private MainFrame parentFrame;
    private JTextField jtfHeight = new JTextField();
    private JTextField jtfWidth = new JTextField();
    private JButton jbtnOK = new JButton("OK");
    private JButton jbtnCancel = new JButton("Cancel");
    private JColorChooser jColorChooser = new JColorChooser();

    private boolean closeFlag;

    static final String OK_COMMAND = "OK_COMMAND";
    static final String CANCEL_COMMAND = "CANCEL_COMMAND";

    private int w;
    private int h;

    public GridDialog(MainFrame parentFrame) {
        this.parentFrame = parentFrame;
        setLocationRelativeTo(parentFrame);
        setSize(600, 300);
        setResizable(false);
        setTitle("Grid parameters");
        setModal(true);
        setVisible(false);

        jbtnOK.addActionListener(this);
        jbtnOK.setActionCommand(OK_COMMAND);

        jbtnCancel.addActionListener(this);
        jbtnCancel.setActionCommand(CANCEL_COMMAND);

        GridBagLayout gb = new GridBagLayout();
        setLayout(gb);
        
        jColorChooser.setColor(Color.BLACK);
        AbstractColorChooserPanel[] p =  jColorChooser.getChooserPanels();
        for (int i = 1; i < p.length; i++)
        {
            jColorChooser.removeChooserPanel(p[i]);
        }
        //jColorChooser.removeChooserPanel(JColorChooser.)
       // jColorChooser.remove(2);
        //jColorChooser.remove(3);
        //jColorChooser.remove(4);
        //jColorChooser.remove(5);
        jColorChooser.setPreviewPanel(new JPanel());

        JLabel jlWidth = new JLabel("Set width step: ");
        JLabel jlHeight = new JLabel("Set height step: ");
        JLabel jlColor = new JLabel("Select grid color: ");
        gb.setConstraints(jlWidth, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 0), 0, 0));
        add(jlWidth);
        gb.setConstraints(jtfWidth, new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jtfWidth);

        gb.setConstraints(jlHeight, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 0), 0, 0));
        add(jlHeight);
        gb.setConstraints(jtfHeight, new GridBagConstraints(0, 3, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jtfHeight);

        gb.setConstraints(jlColor, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 0), 0, 0));
        add(jlColor);
        gb.setConstraints(jColorChooser, new GridBagConstraints(0, 5, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jColorChooser);
        
        gb.setConstraints(jbtnOK, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnOK);
        gb.setConstraints(jbtnCancel, new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnCancel);

    }

    public boolean runDialog() {
        closeFlag = false;
        setVisible(true);
        return  closeFlag;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals(OK_COMMAND)) {
            boolean errFlag = false;
            try
            {
                h = Integer.valueOf(jtfHeight.getText());
            }
            catch (NumberFormatException nfe)
            {
                errFlag = true;
            }
            try
            {
                w = Integer.valueOf(jtfWidth.getText());
            }
            catch (NumberFormatException nfe)
            {
                errFlag = true;
            }
            if (errFlag) {
                JOptionPane.showMessageDialog(this, "Error parameters.", "Error!", JOptionPane.ERROR_MESSAGE);
                return;
            }
            /*if (p <= 0.0f || p >= 1.0f) {
                JOptionPane.showMessageDialog(this, "Probability must be in (0;1).", "Error!", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (min <= 0 || min > 255) {
                JOptionPane.showMessageDialog(this, "Min value must be in (0;255].", "Error!", JOptionPane.ERROR_MESSAGE);
                return;
            }*/
            closeFlag = true;
            setVisible(false);

        }
        if (e.getActionCommand().equals(CANCEL_COMMAND)) {
            setVisible(false);
        }

    }

    public int getW() {
        return w;
    }
    public int getH() {
        return h;
    }
    
    public Color getC() {
        return jColorChooser.getColor();
    }
}

